package recursion

type Fibs struct {
	val map[int]int
}

// 斐波拉契数列问题

func NewFibs(n int) *Fibs {
	return &Fibs{val: map[int]int{}}
}

func (fib *Fibs) Fibonacci(n int) int {
	if fib.val[n] != 0 {
		return fib.val[n]
	}
	if n <= 1 {
		fib.val[1] = 1
		return 1
	} else if n == 2 {
		fib.val[2] = 1
		return 1
	} else {
		res := fib.Fibonacci(n-1) + fib.Fibonacci(n-2)
		fib.val[n] = res
		return res
	}
}

func (fib *Fibs) Print(n int) int {
	return fib.val[n]
}
